package com.xlh.service.conclude.impl;

import cn.hutool.core.util.ObjectUtil;
import com.xlh.common.ResultEnum;
import com.xlh.dao.conclude.helper.BusiFullMarkMapperHelper;
import com.xlh.dto.conclude.BusiFullMarkDTO;
import com.xlh.dto.conclude.BusiFullMarkExceedDTO;
import com.xlh.enums.conclude.LikedSysFunkEnum;
import com.xlh.exception.common.GlobalException;
import com.xlh.pojo.conclude.BusiFullMark;
import com.xlh.service.conclude.ConcludeBusiFullMarkService;
import com.xlh.service.conclude.ConcludeScoreService;
import com.xlh.support.attendance.po.SettingMarkPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;

/**
 * @author sy
 */
@Service
public class ConcludeBusiFullMarkServiceImpl implements ConcludeBusiFullMarkService {

    @Autowired
    private BusiFullMarkMapperHelper busiFullMarkMapperHelper;


    @Override
    public void settingFullMark(SettingMarkPO settingMarkPO, Function<SettingMarkPO ,List<BusiFullMarkDTO>> function) {

        // 1. 分值存入表中
        List<Long> classIdList = settingMarkPO.getClassIdList();
        Long courseId = settingMarkPO.getCourseId();
        Integer fullMark = settingMarkPO.getFullMark();
        Integer degression = settingMarkPO.getDegression();
        Long chapterId = settingMarkPO.getChapterId();

        // 2. 查询或者更新
        BusiFullMark busiFullMark = busiFullMarkMapperHelper.get(settingMarkPO.getBusiType(), courseId, classIdList.get(0),chapterId);

        // 执行更新操作
        if(ObjectUtil.isNotNull(busiFullMark.getId())){
            busiFullMark.setFullMark(fullMark);
            busiFullMark.setDegression(degression);
            busiFullMarkMapperHelper.update( busiFullMark );
        }else{ // 执行插入操作
            busiFullMark.setFullMark(fullMark);
            busiFullMark.setDegression(degression);
            busiFullMarkMapperHelper.insert( busiFullMark );
        }

    }

    @Override
    public BusiFullMarkExceedDTO isExceedFullMark(SettingMarkPO settingMarkPO, Integer score) {

        BusiFullMarkExceedDTO busiFullMarkExceedDTO = new BusiFullMarkExceedDTO();
        // 1. 分值存入表中
        List<Long> classIdList = settingMarkPO.getClassIdList();
        Long courseId = settingMarkPO.getCourseId();

        // 2. 查询或者更新
        BusiFullMark busiFullMark = busiFullMarkMapperHelper.get(settingMarkPO.getBusiType(), courseId, classIdList.get(0), null);
        busiFullMarkExceedDTO.setFullMark(busiFullMark.getFullMark());

        // 3. 进行分数比较
        if(score.compareTo(busiFullMark.getFullMark()) == 1){

            busiFullMarkExceedDTO.setExceedFullMark(true);
            return busiFullMarkExceedDTO;
        }
        busiFullMarkExceedDTO.setExceedFullMark(false);

        return busiFullMarkExceedDTO;
    }
}
